package com.thread.Demo01;

//多线程操作同一个对象 并发
//买火车票
//发现问题,多个线程操作同一个资源的情况下,线程不安全,数据(重复紊乱)
public class TestConCurrent implements Runnable {

    //票数
    private int ticketNums = 10;
    @Override
    public void run() {
        while (true){
            if (ticketNums<=0){
                break;
            }
            //模拟延时
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"拿到了倒数第"+ticketNums--+"张票");//获取当前线程名

        }
    }

    public static void main(String[] args) {

        TestConCurrent t = new TestConCurrent();
        new Thread(t,"小明").start();
        new Thread(t,"小兰").start();
        new Thread(t,"小王").start();
    }
}

    